trigger ClosedTaskEdit on Task (before update) {
    map<string,boolean> check = new map<string,boolean>();
    All_Security_Extensions__c permissionsCS = All_Security_Extensions__c.getinstance(); 
    /*for(All_Security_Extensions__c a: All_Security_Extensions__c.getall().values()){
        check.put(a.SetupOwnerId,a.Can_Edit_Closed_Tasks__c);   
    }*/
    integer i = 0;
    
    Map<integer,Task> OldTask = new Map<integer, Task>();
    Map<integer,string> Old = new Map<integer, string>();
    
    for(Task TaskOld :Trigger.old){
        OldTask.put(i, TaskOld);
        if(TaskOld.status == 'Completed')
           {
            Old.put(i,'Completed');
           }
        else
           {
            Old.put(i,'NotCompleted');
           }
           i++;
        }

      i=0;
   
     /*  for( Task TaskNew : Trigger.new){

          if (Old.get(i) == 'Completed'){
                
               //if ( s && TaskNew.CreatedDate < system.now().addMinutes(-1)){
               system.debug('here permissionsCS.Can_edit_closed_tasks__c');
               system.debug(permissionsCS.Can_edit_closed_tasks__c);
                if ( permissionsCS.Can_edit_closed_tasks__c != true && TaskNew.CreatedDate < system.now().addMinutes(-1)){
                    if( TaskNew.WhatID == null ){
                    TaskNew.addError('Editing a closed task is not permitted');
                    }
                    else
                    {
                      if( TaskNew.WhatID <> null && OldTask.get(i).WhatId <> null ){
                        TaskNew.addError('Editing a closed task is not permitted');
                      }
                    }
                }
           }
        i++;
      } */
      system.debug('permissionsCS.C');
      for(Task TaskNew : Trigger.new) {
        system.debug('permissionsCS.C');
        if(Trigger.oldMap.get(TaskNew.id).status == 'Completed'){
                system.debug('permissionsCS.C');
            if ( !permissionsCS.Can_edit_closed_tasks__c  && TaskNew.CreatedDate < system.now().addMinutes(-1)){
                    system.debug('permissionsCS.C');
                    system.debug(permissionsCS.Can_edit_closed_tasks__c);
                    if( TaskNew.WhatID == null ){
                    TaskNew.addError('Editing a closed task is not permitted');
                    }
                    else
                    {
                      if( TaskNew.WhatID <> null && OldTask.get(i).WhatId <> null ){
                        TaskNew.addError('Editing a closed task is not permitted');
                      }
                    }
                }
        }
      }
  
       
}